﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClearingRestartingAutomaton.Base
{
    /// <summary>
    /// The Alphabet class represents the alphabet.
    /// </summary>
    [Serializable]
    public class Alphabet
    {
        #region Constructors

        /// <summary>
        /// Default constructor of this class.
        /// </summary>
        public Alphabet()
        {
            this.Letters = new SortedSet<char>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Set of letters in the alphabet.
        /// </summary>
        public SortedSet<char> Letters { get; protected set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Set the alphabet based on its string representation.
        /// </summary>
        /// <param name="alphabet">String representation of the alphabet.</param>
        public void SetAlphabet(string alphabet)
        {
            string[] parts = alphabet.Split(new char[] { ' ', '\t', '\r', '\n', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
            string alphabetString = String.Join(String.Empty, parts);
            this.SetAlphabet(alphabetString as IEnumerable<char>);
        }

        /// <summary>
        /// Set the alphabet based on the enumeration of letters.
        /// </summary>
        /// <param name="letters">Enumeration of letters of the alphabet.</param>
        public void SetAlphabet(IEnumerable<char> letters)
        {
            this.Letters.Clear();
            this.Letters.UnionWith(letters);
        }

        /// <summary>
        /// Decides, whether the given input word is from this alphabet or not.
        /// </summary>
        /// <param name="word">Word.</param>
        /// <returns>True, if the given input word is from this alphabet, otherwise false.</returns>
        public bool IsFromThisAlphabet(string word)
        {
            return word.All(letter => this.Letters.Contains(letter));
        }

        #endregion

        /// <summary>
        /// Returns a System.String that represents this instance.
        /// </summary>
        /// <returns>A System.String that represents this instance.</returns>
        public override string ToString()
        {
            return "{ " + String.Join(", ", this.Letters) + " }";
        }
    }
}
